﻿<%@ WebHandler Language="C#" Class="addAjax" %>

using System;
using System.Web;

public class addAjax : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        string ret = ""; //1:表明sn已存在不能添加,0不存在,可以添加.
        if (context.Request.QueryString["sn"] != null)  //$.get("add.ashx",{sn:sn},function(data){});
        {
            NVR.BLL.DeviceInfo dev = new NVR.BLL.DeviceInfo();
            NVR.Entity.DeviceInfo devInfo = new NVR.Entity.DeviceInfo();
            string sn = context.Request.QueryString["sn"].ToString();
            devInfo = dev.GetDeviceInfo(sn);
            if (devInfo == null) //先判断是否在DeviceInfo表中存在，这样可知设备是否已进入系统
            {
                ret = "2";   //设备没有进入系统。
            }
            else
            {
                NVR.BLL.NVRDevInfo nvr = new NVR.BLL.NVRDevInfo();
                NVR.Entity.NVRDeviceInfo nvrInfo = new NVR.Entity.NVRDeviceInfo();
                nvrInfo = nvr.GetNVRInfoBySn(sn);
                if (nvrInfo == null)
                {
                    ret = "0";  //设备进入系统但没有被用户添加
                }
                else
                {
                    ret = "1";   //设备进入系统但被别的用户添加过
                }
            }
        }
        else if (context.Request.QueryString["username"] != null)//$.get("add.ashx",{username:name},function(){});
        {
            NVR.BLL.UserInfo user = new NVR.BLL.UserInfo();
            NVR.Entity.userInfo userInfo = new NVR.Entity.userInfo();
            string username = context.Request.QueryString["username"].ToString();
            userInfo = user.GetUserInfo(username);
            if (userInfo == null)
            {
                ret = "用户不存在";
            }
            else
            {
                ret = "用户已存在";
            }
        }
        else if (context.Request.QueryString["nvrSn"] != null) //$.get('add.ashx',{nvrSn:nvrSn,snAdmin:snAdmin,;与"sn"区别作为判断条件
        {
            NVR.BLL.NVRDevInfo nvr = new NVR.BLL.NVRDevInfo();
            NVR.Entity.NVRDeviceInfo nvrInfo = new NVR.Entity.NVRDeviceInfo();
            nvrInfo.deviceSN = context.Request.QueryString["nvrSn"].ToString();
            nvrInfo.deviceAdmin = context.Request.QueryString["snAdmin"].ToString();
            nvrInfo.devicePwd = context.Request.QueryString["snPwd"].ToString();
            nvrInfo.deviceLoc = context.Request.QueryString["snLoc"].ToString();
            nvrInfo.username = context.Request.QueryString["snUsername"].ToString();
            string kind = context.Request.QueryString["kind"].ToString();//确定是添加还是修改。
            nvrInfo.addDate = DateTime.Now;
            if (kind == "添加设备")
            {
                if (nvr.AddNvr(nvrInfo) == 1)
                {
                    ret = "添加设备成功";
                }
                else
                {
                    ret = "添加设备不成功";
                }
            }
            if (kind == "确认修改")
            {
                if (nvr.UpdateNvr(nvrInfo) == 1)
                {
                    ret = "修改设备信息成功";
                }
                else
                {
                    ret = "修改设备信息不成功";
                }
            }
        }
        context.Response.Write(ret);
    }
 
    public bool IsReusable{
        get {
            return false;
        }
    }
}